//
//  GTWeChatDynamicLinkView.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicLinkView.h"
#import "GTWeChatDynamicTool.h"
#import "GTWeChatDynamicSettings.h"
#import "GTWeChatDynamicItemModel.h"
#import "RanTestModel.h"
#import "GTWeChatDynamicTuple.h"
#import <TYAttributedLabel/TYAttributedLabel.h>

#pragma mark - ------------------------Model------------------------

@interface GTWeChatDynamicLinkViewModel: NSObject

@property(nonatomic, strong)RanTestModel *sourceModel;
@property(nonatomic, assign)CGFloat theWidth;

@property(nonatomic, assign)CGRect linkLabelFrame;

@property(nonatomic, strong)TYTextContainer *linkTextContainer;

@end

@implementation GTWeChatDynamicLinkViewModel

@end

#pragma mark - ------------------------View------------------------

@interface GTWeChatDynamicLinkView()

@property(nonatomic, strong)TYAttributedLabel *linkLabel;

@end

@implementation GTWeChatDynamicLinkView

#pragma mark - Protocol

+ (GTWeChatDynamicTuple *)getProcessedDataFromSourceData:(id)sourceData withWidth:(CGFloat)theWidth
{
    GTWeChatDynamicLinkViewModel *model = [[GTWeChatDynamicLinkViewModel alloc] init];
    model.sourceModel = (RanTestModel *)sourceData;
    CGFloat height;
    
    CGFloat rowHeight = [GTWeChatDynamicTool getTextHeightWithAttribute:[GTWeChatDynamicSettings linkTextAttributes]];
    
    TYTextContainer *container;
    
    if (model.sourceModel.linkOnes.count){
        
        container = [[TYTextContainer alloc] init];
        
        UIImage *logoImage = [UIImage imageNamed:@"Logo"];
        [container appendImage: logoImage size:CGSizeMake(rowHeight, rowHeight)];
        
        __block NSString *spaceLeft;
        __block NSString *name;
        [model.sourceModel.linkOnes enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
            if (idx == 0 && idx != model.sourceModel.linkOnes.count - 1) {
                name = [NSString stringWithFormat:@"%@;", dic[@"name"]];
                spaceLeft = @"   ";
            }else if(idx == model.sourceModel.linkOnes.count - 1){
                name = [NSString stringWithFormat:@"%@", dic[@"name"]];
                spaceLeft = @"  ";
            }else{
                name = [NSString stringWithFormat:@"%@;", dic[@"name"]];
                spaceLeft = @"  ";
            }
            [container appendText:spaceLeft];
            [container appendLinkWithText:name linkFont:[GTWeChatDynamicSettings atTextAttributes][NSFontAttributeName] linkColor:[GTWeChatDynamicSettings atTextAttributes][NSForegroundColorAttributeName] underLineStyle:kCTUnderlineStyleNone linkData:dic];
        }];
        container = [container createTextContainerWithTextWidth:theWidth];
        height = container.textHeight;

    }else{
        container = nil;
        height = 0;
    }
    
    CGFloat linkLabelX = 0;
    CGFloat linkLabelY = 0;
    CGFloat linkLabelW = theWidth;
    CGFloat linkLabelH = height;
    
    model.linkLabelFrame = CGRectMake(linkLabelX, linkLabelY, linkLabelW, linkLabelH);
    
    model.linkTextContainer = container;
    GTWeChatDynamicTuple *tuple = [[GTWeChatDynamicTuple alloc] init];
    tuple.data = model;
    tuple.height = height;
    return tuple;
}

- (void)feedbackStoreData:(id)data
{
    GTWeChatDynamicLinkViewModel *model = (GTWeChatDynamicLinkViewModel *)data;
    self.linkLabel.frame = model.linkLabelFrame;
    self.linkLabel.textContainer = model.linkTextContainer;
}

#pragma mark - Getter

- (TYAttributedLabel *)linkLabel
{
    if (!_linkLabel) {
        self.linkLabel = ({
            TYAttributedLabel *linkLabel = [[TYAttributedLabel alloc] init];
            [self addSubview:linkLabel];
            linkLabel;
        });
    }
    return _linkLabel;
}

@end
